//
// Copyright (c) 2009 Tridium, Inc.
// Licensed under the Academic Free License version 3.0
//
// History:
//   24 Feb 09  Dan Giorgis  Creation
//

#include "sedona.h"

#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>



////////////////////////////////////////////////////////////////
// Native Methods
////////////////////////////////////////////////////////////////


Cell tutorial1_Add_sum(SedonaVM* vm, Cell* params)
{
  //int32_t a = params[0].fval;
  //int32_t b = params[1].fval;
        int32_t value;   
  	int fd = open("/dev/adc", 0);
	if (fd < 0) {
		perror("open ADC device:");
		return oneCell;
	}
	//for(;;) {
		char buffer[30];
		int len = read(fd, buffer, sizeof buffer -1);
		if (len > 0) {
			buffer[len] = '\0';
			//int value = -1;
			sscanf(buffer, "%d", &value);
			//printf("ADC Value: %d\n", value);
		} else {
			perror("read ADC device:");
			return oneCell;
		}
	//	usleep(500* 1000);
	//}
	
	
  close(fd);
  
  Cell result;
  
  result.ival = value; 
   
  return result;
}












